package com.sgb.kjwl.viewmodel.self.goods;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.MemberPriceEntity;
import com.sgb.kjwl.model.entity.SkuDetail;
import com.sgb.kjwl.model.entity.CommitVipPrice;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class AddMemberGoodsModel extends BaseViewModel {
    //会员价详情
    public MutableLiveData<MemberPriceEntity> detail = new MutableLiveData<>();
    public MutableLiveData<List<MemberPriceEntity.MemberPriceSkuDetail>> detailSku = new MutableLiveData<>();
    //新增商品会员价
    public MutableLiveData<List<SkuDetail.SkuListEntity>> skuList = new MutableLiveData<>();
    public MutableLiveData<SkuDetail> addSkuDetail = new MutableLiveData<>();
    public MutableLiveData<Integer> ShowLoading = new MutableLiveData<>();
    public MutableLiveData<CommitVipPrice> commitData = new MutableLiveData<>();
    public MutableLiveData<Integer> status = new MutableLiveData<>();
    private List<String> list;


    public AddMemberGoodsModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 用户中心 会员价详情
     *
     * @param itemMemberId
     * @param itemId
     */
    public void getMemberPriceDetails(String itemMemberId, String itemId) {
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getGoodsAPI().getMemberPriceDetails("item/member/getItemMemberDetail/" + itemMemberId + "/" + itemId).enqueue(new Callback<BaseEntity<MemberPriceEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<MemberPriceEntity>> call, Response<BaseEntity<MemberPriceEntity>> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response == null || response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    detail.postValue(response.body().getData());
                } else {
                    mToast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<MemberPriceEntity>> call, Throwable t) {
                String msg = t.getMessage();
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 获取sku会员价详情信息
     *
     * @param itemMemberNo
     * @param skuId
     */
    public void getSkuMemberPriceDetail(String itemMemberNo, String skuId) {
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getGoodsAPI().getSkuMemberPriceDetail("item/member/getSkuMemberPriceDetail/" + itemMemberNo + "/" + skuId).enqueue(new Callback<BaseEntity<List<MemberPriceEntity.MemberPriceSkuDetail>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MemberPriceEntity.MemberPriceSkuDetail>>> call, Response<BaseEntity<List<MemberPriceEntity.MemberPriceSkuDetail>>> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response == null || response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    detailSku.postValue(response.body().getData());
                } else {
                    mToast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MemberPriceEntity.MemberPriceSkuDetail>>> call, Throwable t) {
                String msg = t.getMessage();
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 用户中心 会员价详情
     *
     * @param itemId
     */
    public void getSkuList(String itemId) {
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getGoodsAPI().getSkuList("item/member/sku/list/" + itemId).enqueue(new Callback<BaseEntity<SkuDetail>>() {
            @Override
            public void onResponse(Call<BaseEntity<SkuDetail>> call, Response<BaseEntity<SkuDetail>> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response == null || response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    skuList.postValue(response.body().getData().getSkuList());
                    addSkuDetail.postValue(response.body().getData());
                } else {
                    mToast.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<SkuDetail>> call, Throwable t) {
                String msg = t.getMessage();
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }

    /**
     * 提交会员价
     */
    public void commitVipPrice() {
        mLoading.postValue(SHOW_LODING);
        NewAPIManager.getInstance().getGoodsAPI().commitVipPrice(commitData.getValue()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                mLoading.postValue(DISMISS_LODING);
                if (response == null || response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
//                    detail.postValue(response.body().getData());
                    mToast.postValue("提交成功");
                }
                status.postValue(Integer.valueOf(response.body().getCode()));
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                String msg = t.getMessage();
                mLoading.postValue(DISMISS_LODING);
            }
        });
    }
}
